#!/bin/bash

function getProcCount() {
    if [ "$#" = "1" ] then
        local count=`ps -ef | grep $1 | grep -v "grep" | wc | awk '{print $1}'`
        echo $count
    elif [ "$#" = "2" ] then
        local count=`ps -ef | grep $1 | grep $2 | grep -v "grep" | wc | awk '{print $1}'`
        echo $count
    else
        echo "0"
    fi
}


#以MQ为例
function checkMQ() {
    local count=`getProcCount "DemoApp" "java"`
    if [ "$count" == "0" ]
    then
        echo "mq not running! run it"
        nohup /bin/activemq start >/dev/null 2>&1 &
    fi
}

#netty-tcp  
function checkDemoApp() {
    local count=`getProcCount "DemoApp" "java"`
    if [ "$count" == "0" ]
    then
        echo "DemoApp not running! run it"
        nohup ./netty-test/bin/demo-server.sh > log/netty-DemoApp.log 2>&1 &
    fi
}

#netty-test-udp.sh
function checkMyUdpServer() {
    local count=`getProcCount "MyUdpServer" "java"`
    if [ "$count" == "0" ]
    then
        echo "MyUdpServer not running! run it"
        nohup nohup /usr/bin/java -server -classpath /home/emlt/share/netty-test/conf:/home/emlt/share/netty-test/lib/* -Dapp.name=MyUdpServer  -Dapp.repo=/home/emlt/share/netty-test/lib -Dapp.home=/home/emlt/share/netty-test -Dbasedir=/home/emlt/share/netty-test com.careland.netty.demo.MyUdpServer > log/testMyUdpServer.log  2>&1 &
    fi
}

function startMonitor() {
    echo "startMonitor"
    local count=0
    while true
    do
        checkDemoApp
        let count=$count+1
        echo $count
        if [ $count -ge 120 ] # 120 轮是10分钟
        then
            let count=0
        fi
        sleep 5 # 一次sleep5秒
    done
}
